package com.yf.dxapp.module.app.controller.admin.behavior;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.behavior.vo.BehaviorPageReqVO;
import com.yf.dxapp.module.app.controller.admin.behavior.vo.BehaviorRespVO;
import com.yf.dxapp.module.app.controller.admin.behavior.vo.BehaviorSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.behavior.BehaviorDO;
import com.yf.dxapp.module.app.service.behavior.BehaviorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 行为记录")
@RestController
@RequestMapping("/dxapp/behavior")
@Validated
public class BehaviorController {

    @Resource
    private BehaviorService behaviorService;

    @PostMapping("/create")
    @Operation(summary = "创建行为记录")
    @PreAuthorize("@ss.hasPermission('app:behavior:create')")
    public CommonResult<Long> createBehavior(@Valid @RequestBody BehaviorSaveReqVO createReqVO) {
        return success(behaviorService.createBehavior(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新行为记录")
    @PreAuthorize("@ss.hasPermission('app:behavior:update')")
    public CommonResult<Boolean> updateBehavior(@Valid @RequestBody BehaviorSaveReqVO updateReqVO) {
        behaviorService.updateBehavior(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除行为记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('app:behavior:delete')")
    public CommonResult<Boolean> deleteBehavior(@RequestParam("id") Long id) {
        behaviorService.deleteBehavior(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得行为记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('app:behavior:query')")
    public CommonResult<BehaviorRespVO> getBehavior(@RequestParam("id") Long id) {
        BehaviorDO behavior = behaviorService.getBehavior(id);
        return success(BeanUtils.toBean(behavior, BehaviorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得行为记录分页")
    @PreAuthorize("@ss.hasPermission('app:behavior:query')")
    public CommonResult<PageResult<BehaviorRespVO>> getBehaviorPage(@Valid BehaviorPageReqVO pageReqVO) {
        PageResult<BehaviorDO> pageResult = behaviorService.getBehaviorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BehaviorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出行为记录 Excel")
    @PreAuthorize("@ss.hasPermission('app:behavior:export')")
    
    public void exportBehaviorExcel(@Valid BehaviorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BehaviorDO> list = behaviorService.getBehaviorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "行为记录.xls", "数据", BehaviorRespVO.class,
                        BeanUtils.toBean(list, BehaviorRespVO.class));
    }

}